from selenium.webdriver.common.by import By
from frp_page_object.base_page import BasePage, logger
from time import sleep
from selenium.webdriver.support.ui import Select
from selenium.common import NoSuchElementException

class NewSamplesOrderPage(BasePage):
    loc_store = (By.XPATH, '//div[@id="ordSamApplyManageAdd"]//*[text()="门店"]/..//input')
    loc_store_number_input = (By.XPATH, '//div[@id="StoreSelectModal"]//*[text()="编码"]/..//input')
    loc_delivery_date = (By.XPATH, '//*[text()="要求交期"]/..//input')
    loc_liaohao_input = (By.XPATH, '//*[@id="queryStockStockItemList"]//*[text()="料号"]/..//input')
    loc_search_button = (By.XPATH, '')
    loc_order_number = (By.XPATH, '//*[@aria-describedby="upOrdSamApplyManageTableId_samApplicationNo"]')
    # 点击新增按钮

    def __select_store(self, store_number):
        self.click_wait(self.loc_store)
        self.input(self.loc_store_number_input, store_number)
        self.click_wait((By.XPATH, '//*[@id="channelStoreListSelect"]//*[contains(text(),"查询")]'))
        sleep(1)
        self.double_click((By.XPATH, f'//*[@id="channelStoreListSelect"]//form[@id]//*[text()="{store_number}"]'), 8)

    def __input_delivery_date(self, date):
        self.input(self.loc_delivery_date, date)

    def input_form_header(self, store_number, date):
        loc = (By.XPATH, '//*[contains(text(),"新增")]')
        self.move_mouse(loc, 3)
        self.click_wait(loc)
        self.__select_store(store_number)
        self.__input_delivery_date(date)

    def new_product(self, liaohao):
        self.click_wait((By.XPATH, '//div[@id="OrdSamApplyManageAddModel"]//*[contains(text(),"新增")]'))
        self.input(self.loc_liaohao_input, liaohao)
        self.click_wait((By.XPATH, '//*[@id="queryStockStockItemList"]//*[contains(text(),"查询")]'))
        self.double_click((By.XPATH, f'//*[@id="gview_materialListTable"]//*[contains(text(),"{liaohao}")]'), 8)

    def submit(self):
        self.click_wait((By.XPATH, '//*[contains(text(),"保存")]'))
        self.click_wait((By.XPATH, '//*[@id="OrdSamApplyManageEditModel"]//*[contains(text(),"提交")]'))
        # 全产品线
        try:
            self.click((By.XPATH, '//*[@id="prodLineChosListModal"]//*[contains(text(),"顾家工艺")]'), 3)
            self.click((By.XPATH, '//*[@id="prodLineChosListModal"]//button'))
        except NoSuchElementException:
            pass
        order_number = self.locator(self.loc_order_number).get_attribute("title")
        logger.info(f'订单号:    {order_number}')
        return order_number

    def click_audit(self):
        self.click_wait((By.XPATH, '//*[@id="OrdSamApplyManageEditModel"]//*[contains(text(),"审核")]'))
        # 可能有问题
        self.click_wait((By.XPATH, '//div[@class="ivu-modal-wrap"]//button[2]'))
